#!/usr/bin/env python

"""
Build peerscape-VERSION.tgz and peerscape-VERSION.xpi.
"""

from __future__ import with_statement

import os
import sys
import shutil
import subprocess

URL = 'http://peerscape.googlecode.com/svn/trunk/'

if os.path.exists('build'):
    sys.exit('Please remove old build directory first.')

os.umask(022)
os.mkdir('build')
os.chdir('build')

subprocess.check_call(['svn', 'export', '--quiet', URL, 'peerscape'])

execfile('peerscape/peerscape/version.py') # Get definition of VERSION.

pkg = 'peerscape-%s' % VERSION
tgz = 'peerscape-%s.tgz' % VERSION
xpi = 'peerscape-%s.xpi' % VERSION

os.rename('peerscape', pkg)
subprocess.check_call(['tar', 'cfz', tgz, pkg])
os.rename(pkg, 'peerscape')

os.chdir('peerscape/firefox/peerscape-3.x.xpi')
subprocess.check_call(['./build.py'])

os.chdir('../../../..')

os.rename(os.path.join('build', tgz), tgz)
os.rename(os.path.join('build/peerscape/firefox/peerscape-3.x.xpi', xpi), xpi)
shutil.rmtree('build')
